package com.linxianqin.springbootVue.manage;

import com.linxianqin.springbootVue.manage.Async.AsyncManager;
import com.linxianqin.springbootVue.utils.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import net.sf.ehcache.CacheManager;
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;

/**
 * 功能描述：
 *
 * @author lxq
 * @version 1.00
 * @Date 2020/12/13
 */
@Slf4j
@Component
public class ShutdownManager {
    private static CacheManager cacheManager = SpringUtils.getBean(CacheManager.class);
    @PreDestroy
    public void destroy() {
        shutdownEhCacheManager();
        shutdownAsyncManager();
    }

    private void shutdownEhCacheManager()
    {
        try {
            log.info("====关闭缓存====");
            if (cacheManager != null) {
                cacheManager.shutdown();
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    /**
     * 停止异步执行任务
     */
    private void shutdownAsyncManager()
    {
        try
        {
            log.info("====关闭后台任务任务线程池====");
            AsyncManager.me().shutdown();
        }
        catch (Exception e)
        {
            log.error(e.getMessage(), e);
        }
    }
}

